package com.tsll.controller;

import com.tsll.common.AliYunSmsUtils;
import com.tsll.common.StringUtils;
import com.tsll.domain.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 短信发送接口
 * @author ys
 */
@Api("短信发送")
@RestController
public class SendSmsController {

    @Value("${aliyunsms.enable}")
    private boolean enable;
    @Value("${aliyunsms.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyunsms.accessKeySecret}")
    private String accessKeySecret;

    @ApiOperation("单一短信发送")
    @PostMapping("/sendSms")
    public AjaxResult sendSms(@RequestBody SmsRequest smsRequest){
        if(enable && StringUtils.isBlank(smsRequest.getAccessKeyID())){
            smsRequest.setAccessKeyID(accessKeyId);
            smsRequest.setAccessKeySecret(accessKeySecret);
        }
        try{
            //参数校验
            AliYunSmsUtils.validatedRequest(smsRequest);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return AliYunSmsUtils.startSendSms(smsRequest);
    }

    @ApiOperation("批量短信发送")
    @PostMapping("/sendBatchSms")
    public AjaxResult sendBatchSms(@RequestBody BatchSmsRequest batchSmsRequest){
        if(enable && StringUtils.isBlank(batchSmsRequest.getAccessKeyID())){
            batchSmsRequest.setAccessKeyID(accessKeyId);
            batchSmsRequest.setAccessKeySecret(accessKeySecret);
        }
        try{
            //参数校验
            AliYunSmsUtils.validatedBatchRequest(batchSmsRequest);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return AliYunSmsUtils.startSendBatchSms(batchSmsRequest);
    }

    @ApiOperation("单一手机号的发送历史记录")
    @PostMapping("/searchSendHis")
    public AjaxResult searchSendHis(@RequestBody SendHisRequest hisRequest){
        if(enable && StringUtils.isBlank(hisRequest.getAccessKeyID())){
            hisRequest.setAccessKeyID(accessKeyId);
            hisRequest.setAccessKeySecret(accessKeySecret);
        }
        try{
            //参数校验
            AliYunSmsUtils.validatedHisRequest(hisRequest);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return AliYunSmsUtils.startQuerySendDetails(hisRequest);
    }

    @ApiOperation("申请通用短信签名")
    @PostMapping("/startAddSmsSign")
    public AjaxResult startAddSmsSign(@RequestBody SignNameRequest signNameRequest){
        if(enable && StringUtils.isBlank(signNameRequest.getAccessKeyID())){
            signNameRequest.setAccessKeyID(accessKeyId);
            signNameRequest.setAccessKeySecret(accessKeySecret);
        }
        try{
            //参数校验
            AliYunSmsUtils.validatedSignNameRequest(signNameRequest);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return AliYunSmsUtils.startAddSmsSign(signNameRequest);
    }

    @ApiOperation("申请短信模板")
    @PostMapping("/startAddSmsTemplate")
    public AjaxResult startAddSmsTemplate(@RequestBody TemplateNameRequest templateNameRequest){
        if(enable && StringUtils.isBlank(templateNameRequest.getAccessKeyID())){
            templateNameRequest.setAccessKeyID(accessKeyId);
            templateNameRequest.setAccessKeySecret(accessKeySecret);
        }
        try{
            //参数校验
            AliYunSmsUtils.validatedTemplateNameRequest(templateNameRequest);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return AliYunSmsUtils.startAddSmsTemplate(templateNameRequest);
    }

    @ApiOperation("根据签名名称删除短信签名")
    @PostMapping("/startDeleteSmsSign")
    public AjaxResult startDeleteSmsSign(@RequestBody SignNameRequest signNameRequest){
        if(enable && StringUtils.isBlank(signNameRequest.getAccessKeyID())){
            signNameRequest.setAccessKeyID(accessKeyId);
            signNameRequest.setAccessKeySecret(accessKeySecret);
        }
        return AliYunSmsUtils.startDeleteSmsSign(signNameRequest);
    }

    @ApiOperation("根据模板code删除短信模板")
    @PostMapping("/startDeleteSmsTemplate")
    public AjaxResult startDeleteSmsTemplate(@RequestBody TemplateNameRequest templateNameRequest){
        if(enable && StringUtils.isBlank(templateNameRequest.getAccessKeyID())){
            templateNameRequest.setAccessKeyID(accessKeyId);
            templateNameRequest.setAccessKeySecret(accessKeySecret);
        }
        return AliYunSmsUtils.startDeleteSmsTemplate(templateNameRequest);
    }

    @ApiOperation("查询指定名称的短信签名")
    @PostMapping("/startQuerySmsSign")
    public AjaxResult startQuerySmsSign(@RequestBody SmsRequest smsRequest){
        if(enable && StringUtils.isBlank(smsRequest.getAccessKeyID())){
            smsRequest.setAccessKeyID(accessKeyId);
            smsRequest.setAccessKeySecret(accessKeySecret);
        }
        return AliYunSmsUtils.startQuerySmsSign(smsRequest);
    }

    @ApiOperation("查询指定code的短信模板")
    @PostMapping("/startQuerySmsTemplate")
    public AjaxResult startQuerySmsTemplate(@RequestBody SmsRequest smsRequest){
        if(enable && StringUtils.isBlank(smsRequest.getAccessKeyID())){
            smsRequest.setAccessKeyID(accessKeyId);
            smsRequest.setAccessKeySecret(accessKeySecret);
        }
        return AliYunSmsUtils.startQuerySmsTemplate(smsRequest);
    }


}
